#include "sceneTitle.h"
#include "sceneMain.h"
#include "../screen/hud_text.h"
#include "../screen/hud_button.h"
#include "../screen/hud_text.h"
#include "../screen/ui_mouse.h"
#include <cmath>
#include <fstream>
void sceneTitle::init()
{
    scene::init();
    loadData("assets/score.dat");
    SDL_HideCursor();
    game_.stopSound();
    game_.stopMusic();
    game_.playMusic("assets/bgm/Spooky music.mp3");
    hudText::createHUDText(this,"title","ghost escape",game_.getScreenSize() / 2.0f - glm::vec2(0,100),
        glm::vec2(game_.getScreenSize().x / 2.0f, game_.getScreenSize().y / 3.0f));
    SDL_Log("high score: %d",game_.getHighScore());
    hudText::createHUDText(this,"score",std::to_string(game_.getHighScore()).c_str(),
        game_.getScreenSize() / 2.0f + glm::vec2(0,100),glm::vec2(200,50));

    startButton = hudButton::createHUDButton(this,game_.getScreenSize() / 2.0f + glm::vec2(-200,200),"startButton",
        "assets/UI/A_Start1.png",
        "assets/UI/A_Start2.png",
        "assets/UI/A_Start3.png");
    creditsButton = hudButton::createHUDButton(this,game_.getScreenSize() / 2.0f + glm::vec2(0,200),"creditsButton",
        "assets/UI/A_Credits1.png",
        "assets/UI/A_Credits2.png",
        "assets/UI/A_Credits3.png");
    quitButton = hudButton::createHUDButton(this,game_.getScreenSize() / 2.0f + glm::vec2(200,200),"quitButton",
        "assets/UI/A_Quit1.png",
        "assets/UI/A_Quit2.png",
        "assets/UI/A_Quit3.png");
    auto text = game_.loadTextFile("assets/credits.txt");
    credits = hudText::createHUDText(this,"credits",text.c_str(),game_.getScreenSize() / 2.0f,glm::vec2(0),
        "assets/font/VonwaonBitmap-16px.ttf",16);
    mouse = uiMouse::createUiMouse(this,"assets/UI/pointer_c_shaded.png",
        "assets/UI/pointer_c_shaded.png",1.0f,ANCHOR_TOP_LEFT);
    credits->setActive(false);
    credits->setBgSizeByText();
}

void sceneTitle::update(float dt)
{
    scene::update(dt);
    updateColor(dt);
    if(credits->isActive()){
        mouse->update(dt);
        return;
    }
    checkButtonStart();
    checkButtonCredits();
    checkButtonQuit();
}

void sceneTitle::render()
{
    scene::render();
    renderBackground();
}

void sceneTitle::clean()
{
    scene::clean();
}

bool sceneTitle::handleEvents(SDL_Event &event)
{
    if(event.type == SDL_EVENT_MOUSE_BUTTON_UP && credits->isActive()){
        credits->setActive(false);
        return true;
    }
    if(scene::handleEvents(event))return true;
    return false;
}

void sceneTitle::loadData(const char *filename)
{    
    int score = 0;
    std::ifstream file(filename,std::ios::binary);
    if(!file.is_open()){
        SDL_Log("Can't open file %s",filename);
        return;
    }
    file.read(reinterpret_cast<char*>(&score),sizeof(game_.getScore()));
    file.close();
    SDL_Log("score %d,highscore %d",game_.getScore(),game_.getHighScore());
    if(game_.getHighScore() < score)
        game_.setHighScore(score);
}

void sceneTitle::renderBackground()
{
    game_.drawBoundray(glm::vec2(30.0f),game_.getScreenSize() - glm::vec2(30.0f),10.0f, 
        Color_);
}

void sceneTitle::updateColor(float dt)
{
    colorTimer += dt;
    Color_.a = 0.5f + 0.5f * sinf(colorTimer * 0.5f);
    Color_.r = 0.5f + 0.5f * sinf(colorTimer * 0.7f);
    Color_.g = 0.5f + 0.5f * sinf(colorTimer * 0.9f);
}

void sceneTitle::checkButtonStart()
{
    if(startButton->getIsTrigger()){
        SDL_Log("Start");
        game_.safeChangeScene(new sceneMain());
    }
}

void sceneTitle::checkButtonCredits()
{
    if(creditsButton->getIsTrigger()){
        SDL_Log("Credits");
        credits->setActive(!credits->isActive());
    }
}

void sceneTitle::checkButtonQuit()
{
    if(quitButton->getIsTrigger()){
        game_.quit();
        SDL_Log("Quit");
    }
}
